package cn.virens.service.impl.platform.wxmp;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.virens.components.page.PageParamUser;
import cn.virens.database.common.BaseServiceImpl;
import cn.virens.database.common.mapper.ExampleJoin;
import cn.virens.database.common.mapper.ExampleWrapper;
import cn.virens.database.mysql.mapper.wxmp.WxmpBaseMapper;
import cn.virens.database.mysql.model.wxmp.WxmpBase;
import cn.virens.service.inter.platform.wxmp.WxmpBaseService;
import cn.virens.utils.PermissionUtil;
import cn.virens.web.components.open.wxmp.WxMpServiceImpl;
import cn.virens.web.components.open.wxmp.config.WxMpCacheConfigModel;
import cn.virens.web.components.open.wxmp.config.cache.WxMpConfigCache;

/**
 * 公众号 - wxmp_base
 *
 * @author Virens
 * @since  2019-11-09
 */
@Service
public class WxmpBaseServiceImpl extends BaseServiceImpl<WxmpBaseMapper, WxmpBase> implements WxmpBaseService, InitializingBean {
	private @Autowired WxMpConfigCache mWxMpConfigCache;
	private @Autowired WxMpServiceImpl mWxMpService;

	public WxmpBaseServiceImpl() {
		super(WxmpBase.class);
	}

	@Override
	public List<WxmpBase> selectAll() {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.and().andEqualTo("enable", true);

		return mBaseMapper.selectByExample(example);
	}

	@Override
	public WxmpBase insert(WxmpBase record) {
		return super.insert(record);
	}

	@Override
	public WxmpBase update(WxmpBase record) {
		return super.update(record);
	}

	@Override
	public int delete(Serializable... ids) {
		return super.delete(ids);
	}

	@Override
	protected List<?> doPageQuery(PageParamUser param) {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.selectProperties("wxmp_base.id", "wxmp_base.`name`", "wxmp_base.appid", "wxmp_base.secret", "wxmp_base.token", "wxmp_base.aeskey");
		example.selectProperties("wxmp_base.res_group", "wxmp_base.create_time", "wxmp_base.update_time", "sys_user_group.`name` AS res_groupn");
		example.addJoinTable(ExampleJoin.left("sys_user_group", "sys_user_group.identity = wxmp_base.res_group"));

		if (param != null && !PermissionUtil.isCanViewAllData(param)) {
			example.and().andCondition(PermissionUtil.str(param, "wxmp_base."));
		}

		example.and((criteria) -> {
			criteria.andLike("appid", criteriaLike(param, "appid"));
			criteria.andLike("name", criteriaLike(param, "name"));
		});

		return mBaseMapper.selectJoinMap(example);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		ExampleWrapper example = ExampleWrapper.of(clazz);
		example.and().andEqualTo("enable", true);

		for (WxmpBase wxmpBase : mBaseMapper.selectByExample(example)) {
			WxMpCacheConfigModel config = new WxMpCacheConfigModel();
			config.setWxMpConfigCache(mWxMpConfigCache);
			config.setWxmpBase(wxmpBase);

			mWxMpService.addConfigStorage(config);
		}
	}
}
